package com.probiz.estore.catalog.dao.impl;

import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.util.StringUtil;
import com.probiz.estore.catalog.dao.BaseCategoryDao;
import com.probiz.estore.catalog.model.BaseCategory;
import com.probiz.estore.catalog.model.BaseCategorySearchCriteria;
import org.apache.commons.lang.ArrayUtils;

import java.util.List;

/**
 * Created by Probiz
 * User: hoverming
 * Date: 2009-12-2
 * Time: 11:06:46
 */
public class BaseCategoryDaoImpl <T> extends HibernateGenericDaoImpl<T> implements
        BaseCategoryDao<T> {


	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.CategoryDao#getCategoryTree(com.probiz.estore.catalog.model.Category, int, com.probiz.estore.catalog.model.CategorySearchCriteria)
	 */
	public BaseCategory getCategoryTree(BaseCategory parentCategory, int depth, BaseCategorySearchCriteria categorySearchCriteria) {
		if (parentCategory!=null && depth <= 0)
			return parentCategory;
		// start: 查找直系子目录
		String hql = "select "+getCategoryQuerySelectFieldsString()+" from "
				+ entityClass.getName() + " c where ";
		if (null != parentCategory) {
			hql += "c.parent.categoryId =" + parentCategory.getCategoryId();
		} else {
			hql += "c.categoryId = 1";
		}
		if(!ArrayUtils.isEmpty(categorySearchCriteria.getCategoryTypes())){
			String categoryTypeString = StringUtil.arrayToString(categorySearchCriteria.getCategoryTypes(), ",");
			hql += " and c.categoryType in ("+categoryTypeString+") ";
		}
		if(!ArrayUtils.isEmpty(categorySearchCriteria.getStatuses())){
			String categoryStatusString = StringUtil.arrayToString(categorySearchCriteria.getStatuses(), ",");
			hql += " and c.status in ("+categoryStatusString+") ";
		}
		hql += " order by sortOrder asc";
		List list = findByHql(hql);
		// end: 查找直系子目录
		if (parentCategory == null) {
			//根节点是主页
			Object[] array = (Object[]) list.get(0);
			parentCategory = convertCategoryArrayToObject(array);
			return getCategoryTree(parentCategory,  depth,categorySearchCriteria);
		} else {
			// start:将得到的子目录类型转换并设置到父目录中
            for (Object object : list) {
                Object[] array = (Object[]) object;
                BaseCategory category = convertCategoryArrayToObject(array);
                parentCategory.getChildren().add(category);
            }
			// end:将得到的子目录类型转换并设置到父目录中
			// start 迭代查找
			for(BaseCategory category: parentCategory.getChildren()){
				getCategoryTree(category,(depth-1),categorySearchCriteria);
			}
			// end 迭代查找
		}

		return parentCategory;
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.CategoryDao#getCategoryTree(com.probiz.estore.catalog.model.Category, int)
	 */
	public BaseCategory getCategoryTree(BaseCategory parentCategory, int depth){
		return getCategoryTree(parentCategory,depth,new BaseCategorySearchCriteria());
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.CategoryDao#getCategoryById(java.lang.Integer)
	 */
	public BaseCategory getCategoryById(Integer categoryId){
		String hql = "select" +getCategoryQuerySelectFieldsString()+"  from "
			+ entityClass.getName() + " c where c.categoryId="+categoryId;
		Object[] array = (Object[])this.findUnique(hql);
		return convertCategoryArrayToObject(array);
	}

    /*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.CategoryDao#countSubCategoryById(java.lang.Integer)
	 */
	public Integer countSubCategoryById(Integer categoryId) {
		String hql = "select count(c.categoryId) from "+entityClass.getName()+" c where c.parent.categoryId=?";
		Long subCount = (Long) this.findUnique(hql, categoryId);
		return new Integer(subCount.toString());
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.BaseCategoryDao#countCategoryTotal()
	 */
	public Long countCategoryTotal(){
		return this.countByHql("from "+entityClass.getName());
	}

    /**
     * 将Object[]转换为Category类型
     * 目的是为了满足结果的范型。
     * @param array 属性数组
     * @return Category
     */
    private BaseCategory convertCategoryArrayToObject(Object[] array) {
        Integer categoryId = (Integer) array[0];
        String categoryName = (String) array[1];
        String categoryCode = (String) array[2];
        String categoryPath = (String) array[3];
        Short categoryDepth = (Short) array[4];
        Short status = (Short) array[5];
        Short hasChildren = (Short) array[6];
        Integer parentId = null;
        if(array[8]!=null)parentId = (Integer) array[7];
        Integer itemCount = (Integer) array[8];
        Integer activeItemCount = (Integer) array[9];
        Short categoryType = (Short) array[10];
        String displayFlag = (String)array[11];
        String imageUrl = (String)array[12];


        BaseCategory category = new BaseCategory();
        category.setCategoryId(categoryId);
        category.setCategoryName(categoryName);
        category.setCategoryCode(categoryCode);
        category.setCategoryPath(categoryPath);
        category.setDepth(categoryDepth);
        category.setStatus(status);
        category.setHasChildren(hasChildren);
        category.setParentId(parentId);
        category.setItemCount(itemCount);
        category.setActiveItemCount(activeItemCount);
        category.setCategoryType(categoryType);
        category.setDisplayFlag(displayFlag);
        category.setImageUrl(imageUrl);
        return category;
    }

    /**
	 * 获得目录的搜索字段查询字符串
	 * @return 搜索字段查询字符串
	 */
	private String getCategoryQuerySelectFieldsString(){
		return " c.categoryId,c.categoryName,c.categoryCode,c.categoryPath,c.depth,c.status,c.hasChildren,c.parent.categoryId,c.itemCount,c.activeItemCount,c.categoryType,c.displayFlag,c.imageUrl ";
	}
}
